home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 August: Tool Chest / Dev.CD Aug 95 TC / Dev.CD Aug 95 TC.toast / Sample Code / Snippets / Testing & Debugging / DebuggerPresence / debugger.c next >
Encoding:
C/C++ Source or Header  |  1993-08-11  |  4.1 KB  |  145 lines  |  [TEXT/MPS ]

  1. /*
  2. **  This is a small snippet of code that can be used to to detect if
  3. **  macsbug is installed or not. NOTE:  This code is intended to only
  4. **  work with version 6.2 of macsbug.  You should refer to your Low Level
  5. **  Debugger's manual for more information on how they install
  6. **  themselves.
  7. **  
  8. **  This code is based on information obtained from the MacsBug
  9. **  Reference. The basic assumptions are that macsbug will install itself
  10. **  in the following manner:
  11. **  
  12. **  If you are running with a Memory Manager that only works in 24 bit
  13. **  mode, then the high -order byte of MacJmp is a flags byte that
  14. **  contains the following information:
  15. **  
  16. **  Bit    Meaning
  17. **  ---    --------------------------------------
  18. **  7    -   Set if debugger is running
  19. **  6    -   Set if debugger can handle system errors
  20. **  5    -   Set if debugger is installed
  21. **  4     -   Set if debugger can support discipline utility
  22. **  
  23. **  The lower 3 bytes are used to store the address of the debugger's
  24. **  entry point.
  25. **  
  26. **  If you are running with a Memory Manager that works in 32-bit mode,
  27. **  the flags byte is moved to address 0xBFF and the long word at MacJmp
  28. **  becomes a full 32-bit address that points to the debugger's entry
  29. **  point..
  30. **  
  31. **  Symantec has a comment in the Think Reference 2.0.1 which states:
  32. **  
  33. **  "ADDENDUM:  The above information seems to be incorrect in the
  34. **  reference manual. I have found through testing etc. that in both
  35. **  modes, the Flag Byte appears at location 0xBFF.  The code reflects
  36. **  these findings."
  37. **  
  38. **  This is because they confused running in 24 bit mode, running in 32
  39. **  bit mode, and the _ability_ to run in 32 bit mode.  It is the latter
  40. **  ability which you must test to determine the location of the debugger
  41. **  flags.
  42. **
  43. */
  44.  
  45. #define TEST    // if you want a standalone procedure, undefine TEST
  46.  
  47. #include <Memory.h>
  48. #include <GestaltEqu.h>
  49. #include <SysEqu.h>
  50.  
  51. /* stuff that should be defined in C, but isn't (it's in Private.a) */
  52. #define    MacJmp            (Ptr *)  0x120        /* MACSBUG jumptable [pointer] */
  53. #define    MacJmpByte        (char *) 0x120        /* MACSBUG flags in 24 bit mode [byte] */
  54. #define MacJmpFlag        (char *) 0xBFF        /* MacsBug flag [byte] */
  55.  
  56. enum dTypes {
  57.     noDebugger,
  58.     macsbug,
  59.     tmon,
  60.     other
  61. };
  62.  
  63. typedef enum dTypes    debuggerTypes;
  64.  
  65. /* stuff for use in this code */
  66. #define    DebuggerInstalled    5
  67.  
  68. /*
  69. ** GetDebuggerInfo
  70. ** as documented in the "Macsbug Reference & Debugging Guide", page 412
  71. ** if we have a 32 bit capable Memory Manager, debugger flags are at $BFF
  72. ** if we have a 24 bit capable Memory Manager, debugger flags are at $120
  73. ** Ptr at $120 
  74. **
  75. ** Note that the documentation is slightly obscure--you check if the machine
  76. ** is capable of 32-bit mode, _not_ if you are running in 32 bit mode.
  77. **/
  78. Boolean
  79. GetDebuggerInfo(debuggerTypes *kind, short *signature)
  80. {
  81.     short    debugFlags;
  82.     Ptr        debugEntry;
  83.     short    **debugWorld;
  84.     Ptr        ROMBaseWorld;
  85.     long    addressingMode;
  86.     
  87.     /* initialize defaults, assuming no debugger present */
  88.     *kind = noDebugger;
  89.     *signature = '  ';
  90.  
  91.     Gestalt(gestaltAddressingModeAttr, &addressingMode);
  92.     if (addressingMode & (1 << gestalt32BitCapable))
  93.         debugFlags = *MacJmpFlag;
  94.     else
  95.         debugFlags = *MacJmpByte;
  96.  
  97.     if ( debugFlags & (1 << DebuggerInstalled) ) {
  98.  
  99.         /* we've got a debugger. what is it? */
  100.         debugEntry = StripAddress(*MacJmp);
  101.         ROMBaseWorld = StripAddress((Ptr)*(long *)ROMBase);
  102.  
  103.         if (debugEntry < ROMBaseWorld) {    /* not ROM based debugger */
  104.  
  105.             debugWorld = (short **)StripAddress(debugEntry - sizeof(Ptr));
  106.             *signature = **debugWorld;
  107.             switch (*signature) {
  108.                 case 'MT':
  109.                     *kind = macsbug;
  110.                     break;
  111.                 case 'WH':
  112.                     *kind = tmon;
  113.                     break;
  114.                 default:
  115.                     *kind = other;
  116.                     break;
  117.             }
  118.         }
  119.         return true;
  120.     }
  121.     return false;
  122. }
  123.  
  124. #ifdef TEST
  125.  
  126. #include <stdio.h>
  127.  
  128. main()
  129. {
  130.     debuggerTypes    k;
  131.     union {
  132.         short    sigShort;
  133.         char    sigChar[2];
  134.     } s;
  135.     char    *dbgTypes[] = {"no debugger", "Macsbug", "TMON", "other debugger"};
  136.     printf("Test of debugger info\n");
  137.     
  138.     if (GetDebuggerInfo(&k, &s.sigShort))
  139.         printf("Debugger type is %s and debugger signature is %c%c",
  140.             dbgTypes[k], s.sigChar[0], s.sigChar[1]);
  141.     else
  142.         printf("Didn't find a debugger.\n");
  143. }
  144.  
  145. #endif